package com.hengpeng.msls.service.client.shortcut.alipay;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.hengpeng.msls.port.client.vo.client.ClientType;
import com.hengpeng.msls.service.client.shortcut.ShortcutBaseService;
import com.hengpeng.msls.util.CommUtil;

/**
 * 支付宝快捷登陆
 * @author wuhaifei
 * @version 1.0 2012-12-28
 */
public class AlipayServiceImpl implements ShortcutBaseService
{

	@Override
	public String shortcutRequest(Map<String, String> params)
	{
		Map<String, String> sParaTemp = new HashMap<String, String>();

		sParaTemp.put("service", AlipayConfig.SERVICE);
		sParaTemp.put("login_service", AlipayConfig.LOGIN_SERVICE);
		sParaTemp.put("partner", AlipayConfig.PARTNER);
		sParaTemp.put("sign_type", AlipayConfig.SIGN_TYPE);
		sParaTemp.put("input_charset", AlipayConfig.INPUT_CHARSET);
		sParaTemp.put("return_url", params.get("return_url"));
		sParaTemp.put("return_url_failed", params.get("return_url"));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		sParaTemp.put("request_id", sdf.format(new Date()));
		String sign = AlipayCore.buildMysign(AlipayCore.paraFilter(sParaTemp), null);
		sParaTemp.put("sign", sign);

		String method = AlipayConfig.METHOD_POST;
		String clientType = params.get("clientType");
		if (ClientType.ANDROID.name().equals(clientType))
		{
			method = AlipayConfig.METHOD_URL;
		}

		return CommUtil.buildForm(sParaTemp, AlipayConfig.ALIPAY_GATEWAY_NEW, method, "submit");
	}

	@Override
	public Map<String, String> shotcutResponse(Map<String, String[]> params)
	{
		Map<String, String> conParams = CommUtil.convertCharset(params);
		String sign = conParams.get("sign");
		String signLocal = AlipayCore.buildMysign(AlipayCore.paraFilter(conParams), null);
		if (!CommUtil.isEmpty(signLocal) && signLocal.equals(sign))
		{
			conParams.put("oprate_result", "true");
		}
		return conParams;
	}

}
